﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1.ConfigCmd
{
    public partial class FormIPconfig : Form
    {
        private string devId;
        private string monitorId;
        private string monitorId_Name;
        private string ip_port;
       
        public FormIPconfig(string deviceId, string MonitorDev_Id, string monitorDevId_Name)
        {
            InitializeComponent();

            devId = deviceId;
            monitorId = MonitorDev_Id;
            monitorId_Name = monitorDevId_Name;

            // 获取IP，端口地址
            textBox1.Text = ConfigCmd.configCommand.ip.ip1;
            textBox2.Text = ConfigCmd.configCommand.ip.ip2;
            textBox3.Text = ConfigCmd.configCommand.ip.ip3;
            textBox4.Text = ConfigCmd.configCommand.ip.ip4;

            textBox5.Text = ConfigCmd.configCommand.ip.port;
        }

        private void button_enter_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.textBox1.Text != "" && this.textBox2.Text != "" && this.textBox3.Text != "" &&
                    this.textBox4.Text != "" && this.textBox5.Text != "")
                {
                    try
                    {
                        int IP1 = Convert.ToInt32(this.textBox1.Text);
                        int IP2 = Convert.ToInt32(this.textBox2.Text);
                        int IP3 = Convert.ToInt32(this.textBox3.Text);
                        int IP4 = Convert.ToInt32(this.textBox4.Text);
                        int Port = Convert.ToInt32(this.textBox5.Text);

                        // 不够3位字符的，前面补0
                        string temp = IP1.ToString();
                        if (temp.Length < 3)
                        {
                            int gap = 3 - temp.Length;
                            string zero = "";
                            for(int g = 0;g<gap; ++g)
                            {
                                zero += "0";
                            }

                            temp = zero + temp;
                        }
                        else if (temp.Length > 3)
                        {
                            MessageBox.Show("请输入正确IP格式！", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }

                        ip_port += temp;  

                        // 不够3位字符的，前面补0
                        temp = IP2.ToString();
                        if (temp.Length < 3)
                        {
                            int gap = 3 - temp.Length;
                            string zero = "";
                            for(int g = 0;g<gap; ++g)
                            {
                                zero += "0";
                            }

                            temp = zero + temp;
                        }
                        else if (temp.Length > 3)
                        {
                            MessageBox.Show("请输入正确IP格式！", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }

                        ip_port += temp;  

                        // 不够3位字符的，前面补0
                        temp = IP3.ToString();
                        if (temp.Length < 3)
                        {
                            int gap = 3 - temp.Length;
                            string zero = "";
                            for(int g = 0;g<gap; ++g)
                            {
                                zero += "0";
                            }

                            temp = zero + temp;
                        }
                        else if (temp.Length > 3)
                        {
                            MessageBox.Show("请输入正确IP格式！", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }

                        ip_port += temp;  

                        // 不够3位字符的，前面补0
                        temp = IP4.ToString();
                        if (temp.Length < 3)
                        {
                            int gap = 3 - temp.Length;
                            string zero = "";
                            for(int g = 0;g<gap; ++g)
                            {
                                zero += "0";
                            }

                            temp = zero + temp;
                        }
                        else if (temp.Length > 3)
                        {
                            MessageBox.Show("请输入正确IP格式！", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }

                        ip_port += temp;  

                        // 不够3位字符的，前面补0
                        temp = Port.ToString();
                        if (temp.Length < 4)
                        {
                            int gap = 3 - temp.Length;
                            string zero = "";
                            for(int g = 0;g<gap; ++g)
                            {
                                zero += "0";
                            }

                            temp = zero + temp;
                        }
                        else if (temp.Length > 4)
                        {
                            MessageBox.Show("请输入正确端口格式！", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }

                        ip_port += temp;  

                    
                    }
                    catch (Exception Ex)
                    {
                        MessageBox.Show("请输入正确IP格式！", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    
                    
                }
                else
                {

                    MessageBox.Show("请输入完整IP！", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                string ipDetail = this.textBox1.Text + "." + this.textBox2.Text + "." + this.textBox3.Text + "." +
                                  this.textBox4.Text + ":" + this.textBox5.Text;
                if (ConfigCmd.configCommand.instance.SetDeiviceIP_Port(devId, monitorId, ip_port))
                {
                    UserLog.adduseroperlog(User.UserID, User.Username, "指定IP地址端口号", "成功", monitorId_Name, devId, ipDetail);
                    MessageBox.Show("已进入发送队列！", "", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    // 记录IP，端口地址
                    ConfigCmd.configCommand.ip.ip1 = textBox1.Text;
                    ConfigCmd.configCommand.ip.ip2 = textBox2.Text;
                    ConfigCmd.configCommand.ip.ip3 = textBox3.Text;
                    ConfigCmd.configCommand.ip.ip4 = textBox4.Text;

                    ConfigCmd.configCommand.ip.port = textBox5.Text;

                    this.Dispose();
                }
                else
                {
                    UserLog.adduseroperlog(User.UserID, User.Username, "指定IP地址端口号", "失败", monitorId_Name, devId, ipDetail);
                    MessageBox.Show("发送失败!", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void button_cancle_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void textBox5_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                button_enter_Click(this, e);
            }
        }

        private void FormIPconfig_Load(object sender, EventArgs e)
        {

        }
    }
}
